/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tdtest.action;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import tdtest.TDTest;
import tdtest.data.*;

/**
 *
 * @author sutter
 */
public class NameEditAction implements Action {
	
	private TDSystemComponent sysComp;
	
	public NameEditAction(TDSystemComponent sysComp) {
		this.sysComp = sysComp;
	}
	
	@Override
	public void run() {
		Stage stage = loadDialog();
		stage.show();
	}
	
	private Stage loadDialog() {
		
		final Stage dialog = new Stage(StageStyle.TRANSPARENT);
		dialog.initModality(Modality.WINDOW_MODAL);
		dialog.initOwner(TDTest.getMainStage());
		
		//create the layout
		GridPane grid = new GridPane();
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(25, 25, 25, 25));
		
		//add the title
		Text scenetitle = new Text("Welcome");
		scenetitle.setId("welcome-text");
		grid.add(scenetitle, 0, 0, 2, 1);

		//user name field and label
		Label label = new Label("Name:");
		grid.add(label, 0, 1);

		final TextField nameField = new TextField();
		nameField.setText(sysComp.name);
		grid.add(nameField, 1, 1);
		
		//add a button - Use the hbox so we can use a different alignment for button
		Button okButton = new Button("OK");
		okButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent e) {
				sysComp.name = nameField.getText();
				sysComp.onUpdate();
				dialog.hide();
			}
		});
		grid.add(okButton, 1, 2);
		
		Button cancelButton = new Button("Cancel");
		cancelButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent e) {
				dialog.hide();
			}
		});
		grid.add(cancelButton, 2, 2);

		//create the scene
		Scene scene = new Scene(grid, 300, 275);
		dialog.setScene(scene);
		
		return dialog;
	}
}
